﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
		<title>Hangfire Integration</title>
		<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
	</head>

	<body>

		<div class="document-contents">

			<h3>
				Introduction</h3>
			<p>
				<a href="http://hangfire.io/" target="_blank">Hangfire</a> is a 
	compherensive background job manager. You can <strong>integrate</strong> ASP.NET Boilerplate 
	with Hangfire to use it instead of 
				<a href="/Pages/Documents/Background-Jobs-And-Workers">default background job manager</a>. You can 
	use the <strong>same background job API</strong> for Hangfire. Thus, your code will be 
				<strong>independent</strong> of Hangfire. But, if you like, you can directly use 
				<strong>Hangfire's 
	API</strong> also.</p>
			<p>
				Hangfire Integration depends on the frameworks you are using.</p>
			<h3>
				ASP.NET Core Integration</h3>
			<p>
				<a href="https://www.nuget.org/packages/Abp.HangFire.AspNetCore" target="_blank">
				<strong>Abp.HangFire.AspNetCore</strong></a> package is used to integrate to 
				ASP.NET Core based applications. It depends on
				<a href="https://www.nuget.org/packages/Hangfire.AspNetCore/" target="_blank">
				Hangfire.AspNetCore</a>. 
				<a href="https://www.hangfire.io/blog/2016/07/16/hangfire-1.6.0.html" target="_blank">
				This document</a> describes to install hangfire to an ASP.NET 
				Core project. It's similar for ABP based projects too. First install
				<a href="https://www.nuget.org/packages/Abp.HangFire.AspNetCore" target="_blank">
				Abp.HangFire.AspNetCore</a> package to your web project:</p>
<pre>Install-Package Abp.HangFire.AspNetCore</pre>
<p>Then you can install any 
	storage for Hangfire. Most common one is SQL Server storage (see <a href="https://www.nuget.org/packages/Hangfire.SqlServer" target="_blank">
					<strong>Hangfire.SqlServer</strong>
				</a> 
	nuget package). After you installed these nuget packages, you can 
				<strong>configure</strong> your project to use Hangfire.</p>
			<p>First, we are changing Startup class to add Hangfire to 
			dependency injection and configure storage and connection string in 
			the <strong>ConfigureServices</strong> method:</p>
			<pre lang="cs">services.AddHangfire(config =&gt;
{
    config.UseSqlServerStorage(_appConfiguration.GetConnectionString("Default"));
});</pre>
			<p>Then we can add UseHangfireServer call in the <strong>Configure</strong> method:</p>
<pre lang="cs">app.UseHangfireServer();</pre>
			<p>If you want to use hangfire's dashboard, you can add it too:</p>
<pre lang="cs">app.UseHangfireDashboard();</pre>
			<p>If you want to <a href="Authorization.html">authorize</a> the dashboard, you can use 
			AbpHangfireAuthorizationFilter as shown below:</p>
			<pre lang="cs">app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
    Authorization = new[] { new AbpHangfireAuthorizationFilter("MyHangFireDashboardPermissionName") }
});</pre>
			<p>The configuration above is almost standard to integrate hangfire 
			to an ASP.NET Core application. For ABP based projects, we should 
			also configure our web module to replace Hangfire for ABP's default 
			background job manager:</p>
<pre lang="cs">
<strong>[DependsOn(typeof (AbpHangfireAspNetCoreModule))]</strong>
public class MyProjectWebModule : AbpModule
{
    public override void PreInitialize()
    {
        <strong>Configuration.BackgroundJobs.UseHangfire();				</strong>
    }

    //...
}</pre>

			<p>We added <strong>AbpHangfireAspNetCoreModule</strong> as a dependency and used 
	Configuration.BackgroundJobs.<strong>UseHangfire</strong> method to replace 
			Hangfire for ABP's default background job manager.</p>
			<p>Hangfire requires 
	schema creation permission in your database since it creates it's own schema 
	and tables on first run. See <a href="http://docs.hangfire.io/en/latest/" target="_blank">Hangfire 
	documentation</a> for more information.</p>
			<h3>
				ASP.NET MVC 5.x Integration</h3>
			<p>
			<a href="https://www.nuget.org/packages/Abp.HangFire" target="_blank">
					<strong>Abp.HangFire</strong>
				</a> nuget package is used for ASP.NET MVC 5.x projects:</p>
<pre>Install-Package Abp.HangFire</pre>
			<p>Then you can install any 
	storage for Hangfire. Most common one is SQL Server storage (see <a href="https://www.nuget.org/packages/Hangfire.SqlServer" target="_blank">
					<strong>Hangfire.SqlServer</strong>
				</a> 
	nuget package). After you installed these nuget packages, you can 
				<strong>configure</strong> your project to use Hangfire as shown below:</p>
			<pre lang="cs">
<strong>[DependsOn(typeof (AbpHangfireModule))]</strong>
public class MyProjectWebModule : AbpModule
{
    public override void PreInitialize()
    {
        <strong>Configuration.BackgroundJobs.UseHangfire(configuration =&gt;
        {
            configuration.GlobalConfiguration.UseSqlServerStorage(&quot;Default&quot;);
        });
				</strong>
    }

    //...
}</pre>
			<p>We added <strong>AbpHangfireModule</strong> as a dependency and used 
	Configuration.BackgroundJobs.<strong>UseHangfire</strong> method to enable 
	and configure Hangfire ("Default" is the connection string in web.config).</p>
			<p>
				Hangfire requires 
	schema creation permission in your database since it creates it's own schema 
	and tables on first run. See <a href="http://docs.hangfire.io/en/latest/" target="_blank">Hangfire 
	documentation</a> for more information.</p>
			<h4>
				Dashboard Authorization</h4>
			<p>
				Hagfire can show a <strong>dashboard page </strong>to see status 
				of all background jobs in real time. You can configure it as 
				described in it's
				<a href="http://docs.hangfire.io/en/latest/configuration/using-dashboard.html" target="_blank">
				documentation</a>. By default, this dashboard page is available 
				for all users, not authorized. You can integrate it to ABP's 
				<a href="Authorization.html">authorization system</a> using <strong>
				AbpHangfireAuthorizationFilter</strong> class defined in 
				Abp.HangFire package. Example configuration:</p>
			<pre lang="cs">app.UseHangfireDashboard(&quot;/hangfire&quot;, new DashboardOptions
{
    Authorization = new[] { <strong>new AbpHangfireAuthorizationFilter()</strong> }
});</pre>
			<p>This checks if current user has logged in to the application. If 
			you want to require an additional permission, you can pass into it's 
			constructor:</p>
			<pre lang="cs">app.UseHangfireDashboard(&quot;/hangfire&quot;, new DashboardOptions
{
    Authorization = new[] { new AbpHangfireAuthorizationFilter(<strong>&quot;MyHangFireDashboardPermissionName&quot;</strong>) }
});</pre>
			<p><strong>Note</strong>: UseHangfireDashboard should be called 
			after authentication middleware in your Startup class (probably as 
			the last line). Otherwise, authorization always fails.</p>

		</div>

	</body>

</html>
